#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        unordered_map<int, int> hash;
        int len = 0, sum = 0;
        hash[0] = -1;
        for (int i = 0; i < nums.size(); i++)
        {
            if (nums[i] == 0) nums[i] = -1;
            sum = sum + nums[i];
            if (hash.count(sum)) len = max(len, i - hash[sum]);
            if (!hash.count(sum)) hash[sum] = i;
        }
        return len;
    }
};